<?php
/**
 * +----------------------------------------------------------------------
 * | summer framework
 * +----------------------------------------------------------------------
 * | Copyright (c) 2020 https://github.com/suframe/think-admin All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: summer <806115620@qq.com>  2020/3/30 14:30
 * +----------------------------------------------------------------------
 */
declare (strict_types = 1);

namespace app\mall\model\traits;


/**
 * 获取门店
 * trait TreeTrait
 * @package app\mall\model\traits
 * @mixin \think\Model
 */
trait TreeTrait
{

    public static function buildTree($menus, $pid = 0, $value = 'id', $label = 'title')
    {
        $rs = [];
        foreach ($menus as $key => $menu) {
            if ($menu['parent_id'] == $pid) {
                $rs[$key] = [
                    'value' => $menu[$value],
                    'label' => $menu[$label],
                ];
                unset($menus[$key]);
                //查找子类
                $child = static::buildTree($menus, $menu['id'], $value, $label);
                if ($child) {
                    $rs[$key]['children'] = array_values($child);
                }
            }
        }
        return $rs;
    }

}